/**
 * @author zhouheng
 * @date 2022年03月13日 14:50
 */

package com.atguigu.seckill.interceptor;

import com.atguigu.common.constant.AuthContant;
import com.atguigu.common.vo.MemberRespVo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginUserInterceptor implements HandlerInterceptor{
    public static ThreadLocal<MemberRespVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
        Object handler) throws Exception {
        HttpSession session = request.getSession();
        // 获取登录用户
        MemberRespVo memberRespVo = (MemberRespVo) session.getAttribute(AuthContant.LOGIN_USER);
        if(memberRespVo != null){
            threadLocal.set(memberRespVo);
            return true;
        }else{
            // 没登陆就去登录
            session.setAttribute("msg", AuthContant.NOT_LOGIN);
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
